//
//  FavoriteViewController.m
//  Coco_Music
//
//  Created by K。 on 15/7/3.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "FavoriteViewController.h"
#import "CollectionData.h"

@interface FavoriteViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation FavoriteViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"我的收藏";
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor whiteColor];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView setBackgroundColor:[UIColor whiteColor]];
    [[CollectionData shareCollectionData]openCollectionData];

    [self.view addSubview:tmpTableView];
    self.myTableView = tmpTableView;
}


-(void)viewWillAppear:(BOOL)animated
{
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[CollectionData shareCollectionData]GetCollection].count == 0) {
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"content-empty"]];
        image.frame = CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.origin.y+64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3);
        [self.view addSubview:image];
        [image release];
    }
    else
    {
        
        return [[CollectionData shareCollectionData]GetCollection].count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str = @"abc";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    Play *play = [[[CollectionData shareCollectionData]GetCollection]objectAtIndex:indexPath.row];
    cell.textLabel.text = play.song_name;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     
    [self add:[[CollectionData shareCollectionData]GetCollection] indexPath:indexPath.row];
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        
        Play *play = [[[CollectionData shareCollectionData]GetCollection]objectAtIndex:indexPath.row];
        [[CollectionData shareCollectionData]deleteCollection:play.song_name];
        [self.myTableView reloadData];
    }
}

@end
